package com.wjerp.tplus.server.domain.service.impl;

import com.wjerp.tplus.server.api.request.BaseRequestPage;
import com.wjerp.tplus.server.api.response.PageDataResponse;
import com.wjerp.tplus.server.domain.entity.CompanyAccountChangeInfo;
import com.wjerp.tplus.server.domain.entity.CompanyAccountInfo;
import com.wjerp.tplus.server.domain.mapper.CompanyAccountChangeInfoMapper;
import com.wjerp.tplus.server.domain.mapper.CompanyAccountInfoMapper;
import com.wjerp.tplus.server.domain.service.CompanyAccountChangeInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author lishuailei
 */
@Service
public class CompanyAccountChangeInfoServiceImpl implements CompanyAccountChangeInfoService {

    @Resource
    private CompanyAccountChangeInfoMapper companyAccountChangeInfoMapper;

    @Resource
    private CompanyAccountInfoMapper companyAccountInfoMapper;

    @Override
    public PageDataResponse<CompanyAccountChangeInfo> loadCompanyAccountChangeInfoByCompanyCode(String companyCode, BaseRequestPage request) {
        PageDataResponse<CompanyAccountChangeInfo> pageDataResponse = new PageDataResponse<>(0, Collections.EMPTY_LIST);
        //获取账户信息
        CompanyAccountInfo companyAccountInfo = companyAccountInfoMapper.selectByCompanyCode(companyCode);
        if(companyAccountInfo == null){
            return pageDataResponse;
        }

        List<CompanyAccountChangeInfo> companyAccountChangeInfoList = companyAccountChangeInfoMapper.selectPageListByAccountCode(companyAccountInfo.getAccountCode(),request.getStart(),request.getPageSize());
        pageDataResponse.setDataList(companyAccountChangeInfoList);
        if(CollectionUtils.isEmpty(companyAccountChangeInfoList)){
            pageDataResponse.setTotal(0);
        }else{
            Integer count = companyAccountChangeInfoMapper.selectPageListCountByAccountCode(companyAccountInfo.getAccountCode());
            pageDataResponse.setTotal(count);
        }
        return pageDataResponse;
    }
}
